package com.weme.live.fragment

import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.jeremyliao.liveeventbus.LiveEventBus
import com.makeramen.roundedimageview.RoundedImageView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.weme.common.base.BaseFragment
import com.weme.common.util.*
import com.weme.common.util.JumpUserManager.jumpCallActivity
import com.weme.common.util.JumpUserManager.jumpUserInfoActivity
import com.weme.common.view.SmoothImageView
import com.weme.live.R
import com.weme.live.databinding.FragmentHistoryAllBinding
import com.weme.live.viewmode.HistorViewMode
import dev.kratos.api.im_server.v1.CallRecord
import dev.kratos.api.im_server.v1.GetCallRecordReply

class AllHistoryFragment(var type: Int) : BaseFragment<FragmentHistoryAllBinding, HistorViewMode>() {

    private var pageIndex: Int = 1
    private var allHistoryList = mutableListOf<CallRecord>()


    override fun startOperate() {
        mBinding.rcvList.setVerticalLayoutManager()
        mBinding.rcvList.adapter = histortAdapter

        //        histortAdapter.addAll(listOf("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""))
        val view = View.inflate(requireContext(), com.weme.common.R.layout.layout_empty_state, null)
        val btRefresh = view.findViewById<TextView>(com.weme.common.R.id.btRefresh)
        btRefresh.setOnClickListener {
            requireActivity().finish()
         LiveEventBus.get<Any>(LiveConfig.NEW_HOME_INDEX).post(1) //            mBinding.smartLayout.autoRefresh()
        }
        histortAdapter.stateView = view //  histortAdapter.setEmptyViewLayout(requireContext(),)
        histortAdapter.submitList(allHistoryList)
        mBinding.smartLayout.autoRefresh()
        mBinding.smartLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageIndex = 1
                when (type) {
                    0 -> viewModel.getCallRecord(pageIndex, type)
                    1 -> viewModel.getCallRecordMissed(pageIndex, type)
                    2 -> viewModel.getCallRecordMatch(pageIndex, type)
                }

            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageIndex++
                when (type) {
                    0 -> viewModel.getCallRecord(pageIndex, type)
                    1 -> viewModel.getCallRecordMissed(pageIndex, type)
                    2 -> viewModel.getCallRecordMatch(pageIndex, type)
                }
            }
        })
        when (type) {
            0 -> {
             LiveEventBus.get<Any>(LiveConfig.NET_WORK_CALL_HISTORY).observe(this) {
                    if (it is GetCallRecordReply) {
                        histortAdapter.isStateViewEnable = true
                        if (pageIndex == 1) {
                            allHistoryList.clear()
                        }
                        allHistoryList.addAll(it.callRecordList)
                        mBinding.smartLayout.finishRefresh()
                        mBinding.smartLayout.finishLoadMore()
                    }
                }
            }
            1 -> {
             LiveEventBus.get<Any>(LiveConfig.NET_WORK_CALL_HISTORY_MISSED).observe(this) {
                    if (it is GetCallRecordReply) {
                        histortAdapter.isStateViewEnable = true
                        if (pageIndex == 1) {
                            allHistoryList.clear()
                        }
                        allHistoryList.addAll(it.callRecordList)
                        mBinding.smartLayout.finishRefresh()
                        mBinding.smartLayout.finishLoadMore()
                    }
                }
            }
            2 -> {
             LiveEventBus.get<Any>(LiveConfig.NET_WORK_CALL_HISTORY_MATCH).observe(this) {
                    if (it is GetCallRecordReply) {
                        histortAdapter.isStateViewEnable = true
                        if (pageIndex == 1) {
                            allHistoryList.clear()
                        }
                        allHistoryList.addAll(it.callRecordList)
                        mBinding.smartLayout.finishRefresh()
                        mBinding.smartLayout.finishLoadMore()
                    }
                }
            }
        }


    }


    private val histortAdapter = object : BaseQuickAdapter<CallRecord, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: CallRecord?) {
            val imCall = holder.getView<ImageView>(R.id.imCall)
            val rimHead = holder.getView<RoundedImageView>(R.id.rimHead)
            val vCircle = holder.getView<View>(R.id.vCircle)
            val imCountry = holder.getView<SmoothImageView>(R.id.imCountry)

            val tvContent = holder.getView<TextView>(R.id.tvContent)
            val imIcon = holder.getView<ImageView>(R.id.imIcon)
            item?.apply {
                rimHead.loadImage(this.toUser.avatar) //1 呼出未接听 2 呼出已接通,3呼入已接通,4呼入未接听，5匹配通话
                holder.setTextColor(R.id.tvName, if (this.type == 4) Color.parseColor("#FF5088") else Color.parseColor("#ffffff"))
                when (this.type) {
                    1 -> {
                        tvContent.text = resources.getString(com.example.resources.R.string.call_history_outgoing_error)
                        imIcon.setImageResource(com.example.resources.R.mipmap.icon_call_out) //   tvContent.setCompoundDrawables(iconOut, null, null, null)
                    }
                    2 -> {
                        tvContent.text = String.format(resources.getString(com.example.resources.R.string.call_history_outgoing), this.duration)
                        imIcon.setImageResource(com.example.resources.R.mipmap.icon_call_out) //     tvContent.setCompoundDrawables(iconOut, null, null, null)com.example.resources.R.mipmap.
                    }
                    3 -> {
                        tvContent.text = String.format(resources.getString(com.example.resources.R.string.call_history_incoming), this.duration)
                        imIcon.setImageResource(com.example.resources.R.mipmap.icon_call_in) //    tvContent.setCompoundDrawables(iconIn, null, null, null)
                    }
                    4 -> {
                        tvContent.text = String.format(resources.getString(com.example.resources.R.string.call_history_missed)) //   tvContent.setCompoundDrawables(iconError, null, null, null)
                        imIcon.setImageResource(com.example.resources.R.mipmap.icon_call_error)
                    }
                    5 -> {
                        tvContent.text = String.format(resources.getString(com.example.resources.R.string.call_history_matched), this.duration)
                        imIcon.setImageResource(com.example.resources.R.mipmap.icon_call_match) //    tvContent.setCompoundDrawables(iconMatch, null, null, null)
                    }
                    else -> {
                        tvContent.text = String.format(resources.getString(com.example.resources.R.string.call_history_missed)) //   tvContent.setCompoundDrawables(iconError, null, null, null)
                        imIcon.setImageResource(com.example.resources.R.mipmap.icon_call_error)
                    }
                }

                holder.setText(R.id.tvName, setNameAge(this.toUser.nickname + " , " + this.toUser.age))
                val country = getCountryBean(this.toUser.countryCode)
                country?.let {
                    Log.e("tag", it.countryFlag.toString())
                    imCountry.loadImage(it.countryFlag.toString())
                }

                when (this.toUser.onlineStatus) {
                    1 -> {
                        vCircle.visible()
                        vCircle.setBackgroundResource(com.example.resources.R.drawable.online_dot)
                    }
                    2 -> {
                        vCircle.visible()
                        vCircle.setBackgroundResource(com.example.resources.R.drawable.busy_dot)
                    }
                    else -> {
                        vCircle.gone()

                    }
                }
                rimHead.setOnFastClickListener {
                    jumpUserInfoActivity(this.toUser.uid,this.toUser.nickname)
                }
                imCall.setOnFastClickListener {
                    jumpCallActivity(this.toUser)
                }
            }


        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_history, parent)
        }


    }

    override fun clickListener() {
    }

    override fun getViewModel(): Class<HistorViewMode> = HistorViewMode::class.java
}